package com.spark.incoicings.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.spark.incoicings.dao.CustomerMapper;
import com.spark.incoicings.dao.SalesOrderMapper;
import com.spark.incoicings.service.CustomerService;
import com.spark.incoicings.service.DictTypeService;
import com.spark.incoicings.vo.DictVO;
import com.spark.invoicings.entity.SaleCustomer;
import com.spark.invoicings.entity.SalesOrder;
import com.spark.invoicings.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.sql.Date;
import java.time.Instant;
import java.time.LocalDate;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 客户信息模块
 */
@RestController
@RequestMapping("/login")
@Slf4j
public class CustomerController {
    @Resource
    private CustomerService customerService;
    @Resource
    private CustomerMapper customerMapper;
    @Resource
    private DictTypeService dictTypeService;
    @Resource
    private SalesOrderMapper salesOrderMapper;
    /**
     * 带条件查询所有客户
     * 分页查询
     * @param
     * @return
     */
    @GetMapping("/selectCustomerAll")
    public Result selectCustomerAll(
            @RequestParam(defaultValue = "1")Integer pageNum,
            @RequestParam(defaultValue = "6")Integer pageSize,
            @RequestParam(value = "customerName",required = false) String customerName,
            @RequestParam(value = "customerPhone",required = false) String customerPhone,
            @RequestParam(value = "time",required = false) String time,
            @RequestParam(value = "date",required = false) String date){
        PageHelper.startPage(pageNum,pageSize);
        Map<String,Object>map = new HashMap<>();
        map.put("customerName",customerName);
        map.put("customerPhone",customerPhone);
        map.put("time",time);
        map.put("date",date);
        List<SaleCustomer> saleCustomers = customerService.selectCustomerAll(map);
        PageInfo<SaleCustomer> page = new PageInfo<>(saleCustomers);
        return Result.success(200,"success",page);
    }
    @GetMapping("/selectDict")
    public Result selectDict(){
        List<DictVO> dict = dictTypeService.selectDict();
        return Result.success(200,"success",dict);
    }
    @GetMapping("/selectCustomerSource")
    public Result selectCustomerSource(){
        List<DictVO> dict = dictTypeService.selectCustomerSource();
        return Result.success(200,"success",dict);
    }
    @GetMapping("/selectCustomerMethod")
    public Result selectCustomerMethod(){
        List<DictVO> dict = dictTypeService.selectCustomerMethod();
        return Result.success(200,"success",dict);
    }
    /**
     * 查询当前客户信息
     * @param id
     * @return
     */
    @GetMapping("/selectCustomer")
    public Result selectCustomer(Integer id){
        SaleCustomer customer = customerService.getById(id);
        return Result.success(200,"success",customer);
    }
    /**
     * 添加客户信息
     * @param customer
     * @return
     */
    @PostMapping("/saveCustomer")
    public Result saveCustomer(@RequestBody SaleCustomer customer){
        log.info("customer={}",customer);
        customerService.save(customer);
        log.info("customer={}",customer);
        return Result.success();
    }
    /**
     * 修改客户信息
     * @param customer
     * @return
     */
    @PostMapping("/updateCustomer")
    public Result updateCustomer(@RequestBody SaleCustomer customer){
        log.info("id{}",customer);
        if (customer.getStartTime()!=null){
            Instant instant =  customer.getStartTime().toInstant();
            ZoneId zoneId = ZoneId.systemDefault();
            ZonedDateTime zonedDateTime = instant.atZone(zoneId);
            LocalDate localDate = zonedDateTime.toLocalDate();
            //上次对账日
            LocalDate lastDate = localDate;
            //下次对账日
            LocalDate endDate = lastDate.plusDays(customer.getReconciliation());
            customer.setStartTime(Date.from(lastDate.atStartOfDay()
                    .atZone(ZoneId.systemDefault()).toInstant()));
            customer.setEndTime(Date.from(endDate.atStartOfDay()
                    .atZone(ZoneId.systemDefault()).toInstant()));
        }
        customerService.update(customer,new QueryWrapper<SaleCustomer>()
                       .eq("customer_id",customer.getCustomerId()));
        return Result.success(200,"success",customer);
    }

    /**
     * 删除客户
     * @return
     */
    @PostMapping("/deleteCustomer")
    public Result deleteCustomer(Integer id){
        List<SalesOrder> salesOrders = salesOrderMapper.selectList(new QueryWrapper<SalesOrder>().eq("customer_id", id));
        if (salesOrders.size()>0){
            return Result.error(400,"当前用户存在订单,不能删除");
        }{
            customerMapper.deleteById(id);
            return Result.success();
        }
    }
}